#include "lcd_init.h"
#include "stdio.h"
#include <string.h>
// #include "app.h"

typedef void (*FunType)(void);
//**********************************write function begin********************************************
void wr_cmd_4spi_8bcs(uint8_t par)
{
    LCD_DCXLow();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    // LCD_DCXHigh();
    // HAL_Delay(5);
}
void wr_dat_4spi_8bcs(uint8_t par)
{
    LCD_DCXHigh();
    LCD_CSLow();

    LCD_SCLLow();
    if (par & 0x80)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x40)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x20)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x10)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x08)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x04)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x02)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_SCLLow();
    if (par & 0x01)
        LCD_SDAHigh();
    else
        LCD_SDALow();
    LCD_SCLHigh();

    LCD_CSHigh();
    LCD_DCXHigh();
    // HAL_Delay(1);
}
void wr_cmd_parall1_8(uint16_t par)
{
    LCD_DCXLow();
    LCD_CSLow();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_dat_parall1_8(uint16_t par)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = par;

    LCD_WRHigh();
}
void wr_cmd_parall2_8(uint16_t par)
{
    LCD_DCXLow();
    LCD_CSLow();
    LCD_WRLow();
    GPIOB->ODR = par << 1;

    LCD_WRHigh();
}
void wr_dat_parall2_8(uint16_t par)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = par << 1;

    LCD_WRHigh();
}
//**********************************read function begin********************************************
void pb_out(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // db 0-7
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
                          GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
void pb_in(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // db 0-7
    GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |
                          GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
uint8_t i801_read_byte(void)
{
    uint8_t v;

    LCD_DCXHigh();
    LCD_RDLow();
    v = GPIOB->IDR;
    LCD_RDHigh();

    return v;
}
void rd_parall1_8(uint8_t c, uint8_t *d, uint32_t l)
{
    uint8_t buf[128];

    wr_cmd_parall1_8(c);
    pb_in();
    for (int i = 0; i < l; i++)
    {
        buf[i] = i801_read_byte();
    }
    LCD_CSHigh();
    pb_out();
    memcpy(d, buf, l);
}
//**********************************write display begin********************************************
void wr_displ_parall1_8(uint8_t color)
{
    LCD_DCXHigh();
    LCD_WRLow();
    GPIOB->ODR = color;
    LCD_WRHigh();
}
void wr_displ_parall2_18(uint16_t color1, uint16_t color2)
{
    LCD_DCXHigh();
    LCD_WRLow();
    if (color1 & 0x02)
        HAL_GPIO_WritePin(DB17_GPIO_Port, DB17_Pin, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(DB17_GPIO_Port, DB17_Pin, GPIO_PIN_RESET);
    if (color1 & 0x01)
        HAL_GPIO_WritePin(DB16_GPIO_Port, DB16_Pin, GPIO_PIN_SET);
    else
        HAL_GPIO_WritePin(DB16_GPIO_Port, DB16_Pin, GPIO_PIN_RESET);
    GPIOB->ODR = color2;
    LCD_WRHigh();
}

void Write(uint8_t dc, uint8_t par)
{
    if (dc == Command)
    {
        wr_cmd_4spi_8bcs(par);
    }
    else if (dc == Parameter)
    {
        wr_dat_4spi_8bcs(par);
    }
}

void HW_Reset(void)
{
    LCD_RSTHigh();
    HAL_Delay(120);
    LCD_RSTLow();
    HAL_Delay(120);
    LCD_RSTHigh();
    HAL_Delay(200);
}

void Delay(uint32_t ms)
{
    HAL_Delay(ms);
}

void SPI_WriteDat_another(uint8_t n)
{
    wr_dat_4spi_8bcs(n);
}

void SPI_WriteCmd_another(uint8_t n)
{
    wr_cmd_4spi_8bcs(n);
}

void Delayms(uint32_t n)
{
    HAL_Delay(n);
}

void initial(void)
{
    LCD_RSTHigh();
    HAL_Delay(120);
    LCD_RSTLow();
    HAL_Delay(120);
    LCD_RSTHigh();
    HAL_Delay(200);

    HAL_Delay(120); // HAL_Delay 120ms

    SPI_WriteCmd_another(0xff);
    SPI_WriteDat_another(0xa5);
    SPI_WriteCmd_another(0x41);
    SPI_WriteDat_another(0x00); // 00:3 SPI 01:2data line SPI
    SPI_WriteCmd_another(0x53);
    SPI_WriteDat_another(0x0f);
    SPI_WriteCmd_another(0x54);
    SPI_WriteDat_another(0x0f);
    SPI_WriteCmd_another(0x55);
    SPI_WriteDat_another(0x5a);
    SPI_WriteCmd_another(0x56);
    SPI_WriteDat_another(0x5a);
    // SPI_WriteCmd_another(0x54);//30
    // SPI_WriteDat_another(0x46);
    //
    // SPI_WriteCmd_another(0x55);//30
    // SPI_WriteDat_another(0xB4);
    //
    // SPI_WriteCmd_another(0x56);
    // SPI_WriteDat_another(0xB4);//30
    SPI_WriteCmd_another(0xA0);
    SPI_WriteDat_another(0x08);
    SPI_WriteDat_another(0x00);
    SPI_WriteCmd_another(0xA1); // stv2
    SPI_WriteDat_another(0x89);
    SPI_WriteCmd_another(0xA2); // stv1
    SPI_WriteDat_another(0x8a);
    SPI_WriteCmd_another(0xA3); // stv4
    SPI_WriteDat_another(0x87);
    SPI_WriteCmd_another(0xA4); // stv3
    SPI_WriteDat_another(0x88);
    SPI_WriteCmd_another(0xA8);
    SPI_WriteDat_another(0x03);
    SPI_WriteCmd_another(0xAA);
    SPI_WriteDat_another(0xb4); /// b4
    SPI_WriteCmd_another(0xB9);
    SPI_WriteDat_another(0x82);
    SPI_WriteCmd_another(0xBA);
    SPI_WriteDat_another(0x83);
    SPI_WriteCmd_another(0xBB);
    SPI_WriteDat_another(0x00);
    SPI_WriteCmd_another(0xBC);
    SPI_WriteDat_another(0x81);
    SPI_WriteCmd_another(0xBD);
    SPI_WriteDat_another(0x86);
    SPI_WriteCmd_another(0xBE);
    SPI_WriteDat_another(0x87);
    SPI_WriteCmd_another(0xBF);
    SPI_WriteDat_another(0x84);
    SPI_WriteCmd_another(0xC0);
    SPI_WriteDat_another(0x85);
    SPI_WriteCmd_another(0xC4); // goa_clk_tchop/goa_clk_tglue[8]/goa_clk_width[3:0]
    SPI_WriteDat_another(0x03);
    SPI_WriteCmd_another(0xC5); // goa_clk_tchop[7:0]
    SPI_WriteDat_another(0x00);
    SPI_WriteCmd_another(0xC6); // goa_clk_tglue[7:0]
    SPI_WriteDat_another(0xb4);
    SPI_WriteCmd_another(0xC7);
    SPI_WriteDat_another(0x01);
    SPI_WriteCmd_another(0xC8);
    SPI_WriteDat_another(0xaa);
    SPI_WriteCmd_another(0xC9);
    SPI_WriteDat_another(0x01);
    SPI_WriteCmd_another(0xCA);
    SPI_WriteDat_another(0x00);
    SPI_WriteCmd_another(0xCB);
    SPI_WriteDat_another(0x03);
    SPI_WriteCmd_another(0xCC);
    SPI_WriteDat_another(0x02);
    SPI_WriteCmd_another(0xCD);
    SPI_WriteDat_another(0x55);
    SPI_WriteCmd_another(0xCE);
    SPI_WriteDat_another(0xfd);
    SPI_WriteCmd_another(0xCF);
    SPI_WriteDat_another(0xfc);
    SPI_WriteCmd_another(0xD0);
    SPI_WriteDat_another(0xFf);
    SPI_WriteCmd_another(0xD1);
    SPI_WriteDat_another(0xFe);
    SPI_WriteCmd_another(0xf2);
    SPI_WriteDat_another(0x46);
    SPI_WriteCmd_another(0x3A);
    SPI_WriteDat_another(0x06); // 05: 565 06:666
    SPI_WriteCmd_another(0x57); // IBIAS
    SPI_WriteDat_another(0x03); // 01: 0.5
    SPI_WriteCmd_another(0x35);
    SPI_WriteCmd_another(0x46);
    SPI_WriteDat_another(0x11);
    SPI_WriteCmd_another(0x47);
    SPI_WriteDat_another(0x22);
    SPI_WriteDat_another(0x23);
    // source
    SPI_WriteCmd_another(0xe6);
    SPI_WriteDat_another(0x22);
    SPI_WriteCmd_another(0xe9);
    SPI_WriteDat_another(0x32);
    SPI_WriteCmd_another(0xeb);
    SPI_WriteDat_another(0x10);
    SPI_WriteCmd_another(0xec);
    SPI_WriteDat_another(0x01);
    SPI_WriteCmd_another(0xed);
    SPI_WriteDat_another(0x12);
    SPI_WriteCmd_another(0xee);
    SPI_WriteDat_another(0x22);
    SPI_WriteCmd_another(0xef);
    SPI_WriteDat_another(0x23);
    SPI_WriteCmd_another(0xf0);
    SPI_WriteDat_another(0x10);
    // pump
    SPI_WriteCmd_another(0x8f); // PUMP CLK SEL
    SPI_WriteDat_another(0x24);
    SPI_WriteDat_another(0x03); // 6 div
    SPI_WriteCmd_another(0x9a); // vsp 6.6v 3*VCI
    SPI_WriteDat_another(0x68); // ddvdh_btrs ddvdh_btvs[1:0] ddvdh_btvs_en
    SPI_WriteCmd_another(0x9b); // vsn -4.4
    SPI_WriteDat_another(0x58);
    SPI_WriteCmd_another(0x9c); // VGH VCI*7
    SPI_WriteDat_another(0xA0);
    SPI_WriteCmd_another(0x9d); // vgh=12V
    SPI_WriteDat_another(0x14);
    SPI_WriteCmd_another(0x9E); // vgl= -12V
    SPI_WriteDat_another(0xC7);
    SPI_WriteCmd_another(0x84); // GVDD ADJ
    SPI_WriteDat_another(0xb2); // GVDD 5.7561
    SPI_WriteCmd_another(0x83); // GVCL ADJ
    SPI_WriteDat_another(0x6e); // GVCL -3.57
    SPI_WriteCmd_another(0x85); // GVSP ADJ
    SPI_WriteDat_another(0x2a); // 3c
    // gamma2.2
    // V63
    SPI_WriteCmd_another(0x60); // V63 vrp0[3:0]
    SPI_WriteDat_another(0x00);
    SPI_WriteCmd_another(0x70); // V63 vrn0[3:0]
    SPI_WriteDat_another(0x00);
    // V0
    SPI_WriteCmd_another(0x6e); // V0 vrp63[3:0]
    SPI_WriteDat_another(0x0f);
    SPI_WriteCmd_another(0x7e); // V0 vrn63[3:0]
    SPI_WriteDat_another(0x0f);
    // V43
    SPI_WriteCmd_another(0x66); // v43 vrp20[6:0]
    SPI_WriteDat_another(0x3b);
    SPI_WriteCmd_another(0x76); // v43 vrn20[6:0]
    SPI_WriteDat_another(0x38);
    // V20
    SPI_WriteCmd_another(0x68); // v20 vrp43[6:0]
    SPI_WriteDat_another(0x4e);
    SPI_WriteCmd_another(0x78); // v20 vrn43[6:0]
    SPI_WriteDat_another(0x4e);
    // V62
    SPI_WriteCmd_another(0x61); // V62 vrp1[5:0]
    SPI_WriteDat_another(0x0c);
    SPI_WriteCmd_another(0x71); // v62 vrn1[5:0]
    SPI_WriteDat_another(0x0c);
    // V61
    SPI_WriteCmd_another(0x62); // v61 vrp2[5:0]
    SPI_WriteDat_another(0x11);
    SPI_WriteCmd_another(0x72); // v61 vrn2[5:0]
    SPI_WriteDat_another(0x13);
    // V2
    SPI_WriteCmd_another(0x6c); // v2 vrp61[5:0]
    SPI_WriteDat_another(0x2e);
    SPI_WriteCmd_another(0x7c); // v2 vrn61[5:0]
    SPI_WriteDat_another(0x2f);
    // V1
    SPI_WriteCmd_another(0x6d); // v1 vrp62[5:0]
    SPI_WriteDat_another(0x37);
    SPI_WriteCmd_another(0x7d); // v1 vrn62[5:0]
    SPI_WriteDat_another(0x37);
    // V59
    SPI_WriteCmd_another(0x63); // v59 vrp4[4:0]
    SPI_WriteDat_another(0x0a);
    SPI_WriteCmd_another(0x73); // v59 vrn4[4:0]
    SPI_WriteDat_another(0x0c);
    // V57
    SPI_WriteCmd_another(0x64); // v57 vrp6[4:0]
    SPI_WriteDat_another(0x0c);
    SPI_WriteCmd_another(0x74); // v57 vrn6[4:0]
    SPI_WriteDat_another(0x07);
    // V50
    SPI_WriteCmd_another(0x65); // v50 vrp13[3:0]
    SPI_WriteDat_another(0x05);
    SPI_WriteCmd_another(0x75); // v50 vrn13[3:0]
    SPI_WriteDat_another(0x06);
    // V36 v27
    SPI_WriteCmd_another(0x67); // v36 vrp36[2:0] v27 vrp27[2:0]
    SPI_WriteDat_another(0x54);
    SPI_WriteCmd_another(0x77); // v36 vrn36[2:0] v27 vrn27[2:0]
    SPI_WriteDat_another(0x24);
    // V13
    SPI_WriteCmd_another(0x69); // v13 vrp50[3:0]
    SPI_WriteDat_another(0x08);
    SPI_WriteCmd_another(0x79); // v13 vrn50[3:0]
    SPI_WriteDat_another(0x09);
    // V6
    SPI_WriteCmd_another(0x6a); // v6 vrp57[4:0]
    SPI_WriteDat_another(0x11);
    SPI_WriteCmd_another(0x7a); // v6 vrn57[4:0]
    SPI_WriteDat_another(0x15);
    // V4
    SPI_WriteCmd_another(0x6b); // v4 vrp59[4:0]
    SPI_WriteDat_another(0x12);
    SPI_WriteCmd_another(0x7b); // v4 vrn59[4:0]
    SPI_WriteDat_another(0x14);
    SPI_WriteCmd_another(0x11); // sleep out
    Delayms(120);
    SPI_WriteCmd_another(0x36);
    SPI_WriteDat_another(0x00); // C0
    SPI_WriteCmd_another(0x29);
    Delayms(20);
}
